﻿using System;
using System.Collections;
using Model;
using Service;
using Service.ReferenceData;
using Resources;

public partial class SupplierAdd : System.Web.UI.Page
{
    readonly ReferenceDataServiceSupplier _ReferenceDataServiceSupplier = new ReferenceDataServiceSupplier();
    private readonly StockServiceSupplier _StockServiceSupplier = new StockServiceSupplier();
    private int _SupplierId     ;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        _SupplierId = GetSupplierId();
        IList list = _ReferenceDataServiceSupplier.GetSupplierById(_SupplierId);
        if(IsPostBack)
            return;
        
        if (list.Count != 0)
        { 
            txtCompanyName.Text = ((Supplier) (list[0])).SupplierName;
            txtVat.Text = ((Supplier) (list[0])).Vat.ToString();
            txtFax.Text = ((Supplier) (list[0])).Fax;
            txtPhone.Text = ((Supplier) (list[0])).Phone;
            txtEmail.Text = ((Supplier) (list[0])).Email;
            dlCategory.SelectedValue = ((Supplier) (list[0])).CategoryId.ToString();
        }
    }
    private void RecallData()
    {
        _SupplierId = GetSupplierId();
        IList list = _ReferenceDataServiceSupplier.GetSupplierById(_SupplierId);
        txtCompanyName.Text = ((Supplier)(list[0])).SupplierName;
        txtVat.Text = ((Supplier)(list[0])).Vat.ToString();
        txtFax.Text = ((Supplier)(list[0])).Fax;
        txtPhone.Text = ((Supplier)(list[0])).Phone;
        txtEmail.Text = ((Supplier)(list[0])).Email;
        dlCategory.SelectedValue = ((Supplier)(list[0])).CategoryId.ToString();
    }
    protected void bntValidate_Click(object sender, EventArgs e)
    {

        if (!new Utility().TestPermissions(Resource.PermissionEditSupplier))
        {
            lblPermission.Text = Resource.Permission;
            RecallData();
            return;
        }

        try
        {
           
            if (dlCategory.SelectedIndex == 0)
            {
                lblRequireCategory.Visible = true;
                return;
            }
            lblRequireCategory.Visible = false;

            if (!string.IsNullOrEmpty(txtVat.Text))
            {
                float f;
                if (float.TryParse(txtVat.Text, out f))
                {
                  
                    lblRequireVat.Visible = false;
                }
                else
                {
                    lblRequireVat.Visible = true;
                    return;
                }
            }

            float vatVal;
            if(!float.TryParse(txtVat.Text, out vatVal))
                return;

            new DirectServiceSupplier().UpdateSupplier(
                _SupplierId, 
                txtCompanyName.Text,
                int.Parse(dlCategory.SelectedItem.Value),
                vatVal, 
                txtFax.Text, 
                txtPhone.Text, 
                txtEmail.Text);
            lblRequireCategory.Visible = false;
            lblRequireVat.Visible = false;

            lblPermission.Text = Resource.MessageValidate; 
            ClearControlAdd();

        }catch (Exception)
        {
        }
    }

    public int GetSupplierId()
    {
        return !String.IsNullOrEmpty(Request.QueryString["companyId"]) ? Int32.Parse(Request.QueryString["companyId"]) : 0;
    }
    private void ClearControlAdd()
    {
        txtCompanyName.Text = "";
        txtEmail.Text = "";
        txtFax.Text = "";
        txtVat.Text = "";
        txtPhone.Text = "";
        dlCategory.SelectedIndex = 0;
 
    }
    
    protected void bntNew_Click(object sender, EventArgs e)
    {
        lblPermission.Text = ""; 
        if (!new Utility().TestPermissions(Resource.PermissionAddSupplier))
        {
            lblPermission.Text = Resource.Permission;
            return;
        }
        Supplier _supplier = new Supplier();
        _supplier.SupplierName = txtCompanyName.Text;

        if (dlCategory.SelectedIndex==0)
        {
            lblRequireCategory.Visible = true;
            return;
        }
        _supplier.CategoryId = Int32.Parse(dlCategory.SelectedItem.Value);
        lblRequireCategory.Visible = false;

        if (!string.IsNullOrEmpty(txtVat.Text))
        {
            float f;
            if (float.TryParse(txtVat.Text, out f))
            {
                _supplier.Vat = float.Parse(txtVat.Text);
                lblRequireVat.Visible = false; 
            }
            else
            {
                lblRequireVat.Visible = true;
                return;
            }
        }

        _supplier.Fax = txtFax.Text;
        _supplier.Phone = txtPhone.Text;
        _supplier.Email = txtEmail.Text;
        _ReferenceDataServiceSupplier.SaveSupplier(_supplier);
         lblRequireCategory.Visible = false;
         lblRequireVat.Visible = false;
        lblPermission.Text = Resource.MessageValidate;  
       
        ClearControlAdd();
    }
}
